package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.candy.controller.vo.AppUserInfo;
import com.wanlian.candy.controller.vo.MsgSendVo;
import com.wanlian.candy.controller.vo.UserPasswordVo;
import com.wanlian.candy.controller.vo.WebUserInfoVo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.common.Verification;
import com.wanlian.entity.*;
import com.wanlian.error.ErrorCode;
import com.wanlian.error.SuccessCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.*;
import com.wanlian.util.DESUtils;
import com.wanlian.util.DateUtil;
import com.wanlian.util.IpUtil;
import com.wanlian.vo.Page;
import com.wanlian.vo.PasswordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserServices userServices;

    @Autowired
    private DeviceServices deviceServices;

    @Autowired
    private TaskServices taskServices;

    @Autowired
    private InvitetServices invitetServices;


    @Autowired
    private UserTaskServices userTaskServices;

    @Autowired
    private WordBookServices wordBookServices;

    @Autowired
    private UserPhoneCodeServices userPhoneCodeServices;

    /**
     * 注册
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/register", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data, HttpServletRequest request) {
        User user = JsonStringUtil.readValue(data, User.class);
        User checkUser = userServices.queryUserByPhone(user.getPhone());
        ResultModel resultModel = new ResultModel();
        if (checkUser == null) {
            user.setLastLoginTime(DateUtil.currentTimestamp());
            user.setManagement(true);
            resultModel = userServices.doAdd(user);
            try {
                user.setUpassword(DESUtils.encryptDES(user.getUserTag(), "123456"));
                userServices.doedit(user);
            } catch (Exception e) {
                System.out.println(e);
            }

            User ruser = userServices.queryUserByPhone(user.getPhone());
            //注册之后直接登录
            String ipAddress = IpUtil.getIpAddress(request);
            HistoryLogin historyLogin = new HistoryLogin();
            historyLogin.setLoginIp(ipAddress);
            historyLogin.setUserid(ruser.getId());
            userServices.doAddLogin(historyLogin);
            //设置登陆session
            UserSessionInfo userSessionInfo = new UserSessionInfo();
            userSessionInfo.setLoginUser(ruser);
            userSessionInfo.addHistoryLogins(historyLogin);
            request.getSession().setAttribute(ConstantCode.USERINFO_KEY, userSessionInfo);
            return new JsonResult().Serailze(resultModel);
        } else {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.REGISTER_DEFAULT);
            return new JsonResult().Serailze(resultModel);
        }
    }

    /**
     * 手机注册
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/register/mobile", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String registerByPhone(@RequestBody String data, HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        MsgSendVo msgSendVo = JsonStringUtil.readValue(data, MsgSendVo.class);
        ResultModel pResultModel = userServices.queryPhoneCodeByPhone(msgSendVo.getMobile());
        if (pResultModel.isBresult() == false) {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CODE_DEFAULT_NOGET);
        } else {
            UserPhoneCode code = (UserPhoneCode) pResultModel.getObject();
            if (code.getCode().equals(msgSendVo.getCode())) {
                code.setEnable(false);
                userPhoneCodeServices.doEditcode(code);
                User user = new User();
                user.setUpassword("123456");
                user.setPhone(msgSendVo.getMobile());
                WordBook wordBook = wordBookServices.queryByVal("default_headimg");
                if (wordBook != null) {
                    user.setHeadImg(wordBook.getWbval1());
                }
                user.setUsername(user.getPhone());
                User checkUser = userServices.queryUserByPhone(msgSendVo.getMobile());
                if (checkUser == null) {
                    user.setManagement(false);
                    resultModel = userServices.doAdd(user);
                    try {
                        user.setUpassword(DESUtils.encryptDES(user.getUserTag(), user.getUpassword()));
                        userServices.doedit(user);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    User loginuser = userServices.queryUserByPhone(user.getPhone());
                    //注册之后直接登录
                    String ipAddress = IpUtil.getIpAddress(request);
                    HistoryLogin historyLogin = new HistoryLogin();
                    historyLogin.setLoginIp(ipAddress);
                    historyLogin.setUserid(loginuser.getId());
                    userServices.doAddLogin(historyLogin);
                    //设置登陆session
                    UserSessionInfo userSessionInfo = new UserSessionInfo();
                    userSessionInfo.setLoginUser(loginuser);
                    userSessionInfo.addHistoryLogins(historyLogin);
                    request.getSession().setAttribute(ConstantCode.USERINFO_KEY, userSessionInfo);
                } else {
                    resultModel.setBresult(false);
                    resultModel.setMessage(ErrorCode.REGISTER_DEFAULT);
                    return new JsonResult().Serailze(resultModel);
                }
                if (resultModel.isBresult()) {
                    User dbUser = userServices.queryUserByPhone(msgSendVo.getMobile());
                    AppUserInfo appUserInfo = new AppUserInfo();
                    BeanUtils.copyProperties(dbUser, appUserInfo);
                    appUserInfo.setSessionId(request.getSession().getId());
                    resultModel.setObject(appUserInfo);
                }
            } else {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CODE_DEFAULT);
            }
        }
        return new

                JsonResult().

                Serailze(resultModel);

    }


    /**
     * 手机注册
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/register/form/mobile/{mobile}/{code}/{sendUserTag}", method = {RequestMethod.GET})
    @ResponseBody
    public String registerByPhone(@PathVariable("mobile") String mobile,
                                  @PathVariable("code") String code,
                                  @PathVariable("sendUserTag") String sendUserTag,
                                  HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setContentType("application/json");

        ResultModel resultModel = new ResultModel();
        ResultModel pResultModel = userServices.queryPhoneCodeByPhone(mobile);
        if (pResultModel.isBresult() == false) {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CODE_DEFAULT_NOGET);
        } else {
            UserPhoneCode codes = (UserPhoneCode) pResultModel.getObject();
            if (codes.getCode().equals(code)) {
                codes.setEnable(false);
                userPhoneCodeServices.doEditcode(codes);
                String passWord = "123456";//Utils.getRandomNumCode(6);
                User user = new User();
                user.setPhone(mobile);
                WordBook wordBook = wordBookServices.queryByVal("default_headimg");
                if (wordBook != null) {
                    user.setHeadImg(wordBook.getWbval1());
                }
                user.setUsername(user.getPhone());
                User checkUser = userServices.queryUserByPhone(mobile);
                if (checkUser == null) {
                    user.setManagement(false);
                    user.setUpassword(passWord);
                    resultModel = userServices.doAdd(user);
                    try {
                        user.setUpassword(DESUtils.encryptDES(user.getUserTag(), user.getUpassword()));
                        userServices.doedit(user);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    User loginuser = userServices.queryUserByPhone(user.getPhone());

                    //若sendUserId不为空，则为邀请送，添加至邀请表
                    User sendUser = userServices.queryUserByTag(sendUserTag);
                    if (sendUser != null) {
                        int sendUserId = sendUser.getId();
                        if (sendUserId != 0) {
                            Task task = taskServices.queryTasksByName("邀请奖励");
                            UserTask userTask = new UserTask();
                            userTask.setAddTime(DateUtil.currentTimestamp());
                            userTask.setUserId(sendUserId);
                            userTask.setTaskId(task.getId());
                            userTaskServices.addUserTask(userTask);
                            if (task.isEnable()) {
                                Invite invite = new Invite();
                                invite.setInviteUserId(loginuser.getId());
                                invite.setRegisterTime(DateUtil.currentTimestamp());
                                invite.setUserId(sendUserId);
                                invite.setPhone(mobile);
                                invitetServices.doAdd(invite);
                            }
                        }
                    }

                } else {
                    resultModel.setBresult(false);
                    resultModel.setMessage(ErrorCode.REGISTER_DEFAULT);
                    return new JsonResult().Serailze(resultModel);
                }

            } else {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CODE_DEFAULT);
            }
        }
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 手机APP端登录
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/login/mobile", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String loginByPhone(@RequestBody String data, HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        MsgSendVo inputVo = JsonStringUtil.readValue(data, MsgSendVo.class);

        User userByPhone = userServices.queryUserByPhone(inputVo.getMobile());
        if (userByPhone == null) {
            resultModel.setBresult(false);
            resultModel.setMessage("该手机号尚未注册，请先注册！");
            return new JsonResult().Serailze(resultModel);
        }
        AppUserInfo appUserInfo = new AppUserInfo();
        ResultModel pResultModel = userServices.queryPhoneCodeByPhone(inputVo.getMobile());
        if (pResultModel.isBresult() == false) {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CODE_DEFAULT);
        } else {
            UserPhoneCode code = (UserPhoneCode) pResultModel.getObject();
            if (!code.isEnable()) {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CODE_DEFAULT);
                return new JsonResult().Serailze(resultModel);
            }
            if (inputVo.getMobile().equals("18666210571")) {
                appUserInfo.setReview(true);
            }
            if (code.getCode().equals(inputVo.getCode())) {
                if (!inputVo.getMobile().equals("18666210571")) {
                    code.setEnable(false);
                    userPhoneCodeServices.doEditcode(code);
                }

                User dbUser = userServices.queryUserByPhone(inputVo.getMobile());
                if (dbUser != null) {
                    if (dbUser.isEnable()) {

                        dbUser.setLastLoginTime(DateUtil.currentTimestamp());
                        userServices.doedit(dbUser);
                        //获取userSessionInfo
                        UserSessionInfo userSessionInfo = new UserSessionInfo();
                        HttpSession session = request.getSession();
                        session.setMaxInactiveInterval(7 * 24 * 60 * 60);
                        String sessionId = session.getId();

                        BeanUtils.copyProperties(dbUser, appUserInfo);
                        ResultModel countResult = deviceServices.queryUserDeviceCount(appUserInfo.getId());
                        if (countResult.isBresult()) {
                            appUserInfo.setDevCount((int) countResult.getCount());
                        }
                        DeviceConfig deviceConfig = deviceServices.queryDeviceConfig(dbUser.getId());
                        if (deviceConfig != null) {
                            appUserInfo.setSord(deviceConfig.getSord());
                        }
                        Device device = deviceServices.queryDeviceByUserId(dbUser.getId());
                        if (device != null) {
                            appUserInfo.setCurrentDevice(device);
                            appUserInfo.setDevicekey(device.getDevicekey());
                            System.out.println("deviceKey:" + device.getDevicekey());
                            System.out.println("phone:" + appUserInfo.getPhone());
                        }
                        appUserInfo.setUserTag(dbUser.getUserTag());
                        appUserInfo.setSessionId(sessionId);
                        appUserInfo.setBindDevices(deviceServices.queryDevicesByUser(dbUser.getId()));
                        userSessionInfo.setLoginUser(dbUser);
                        session.setAttribute(ConstantCode.USERINFO_KEY, userSessionInfo);
                        return new JsonResult().Serailze(appUserInfo);
                    } else {
                        return new JsonResult().Serailze(false, ErrorCode.LOGINDEFAUT_ISENABLE);
                    }

                } else {
                    return new JsonResult().Serailze(false, ErrorCode.LOGIN_DEFAULT);
                }
            } else {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CODE_DEFAULT);
            }
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * web端登录
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String login(@RequestBody String data, HttpServletRequest request, HttpSession session) {
        User user = JsonStringUtil.readValue(data, User.class);
        User db_user = userServices.queryUserByPhone(user.getPhone());
        String pwd = "";
        try {
            pwd = DESUtils.encryptDES(db_user.getUserTag(), user.getUpassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        User dbUser = userServices.queryUserByloginInfo(user.getPhone(), pwd);
        if (dbUser != null) {
            if (dbUser.isEnable()) {
                if(!db_user.getManagement()){
                    return new JsonResult().Serailze(false, ErrorCode.LOGIN_ROLE);
                }
                dbUser.setLastLoginTime(DateUtil.currentTimestamp());
                userServices.doedit(dbUser);

                UserSessionInfo userSessionInfo = new UserSessionInfo();

                ResultModel resultModel2 = userServices.queryLastLoginHistoryById(dbUser.getId());
                if (resultModel2.getObject() != null) {
                    userSessionInfo.addHistoryLogins((HistoryLogin) resultModel2.getObject());
                }
                //添加登陆信息
                String ipAddress = IpUtil.getIpAddress(request);
                HistoryLogin historyLogin = new HistoryLogin();
                historyLogin.setLoginIp(ipAddress);
                historyLogin.setUserid(dbUser.getId());
                userServices.doAddLogin(historyLogin);
                //设置登陆session
               /* userSessionInfo.setLoginUser(dbUser);
                userSessionInfo.addHistoryLogins(historyLogin);
                userSessionInfo.setSessionId(session.getId());
                session.setMaxInactiveInterval(8 * 60 * 60);
                session.setAttribute(ConstantCode.USERINFO_KEY, userSessionInfo);*/
                session.setMaxInactiveInterval(7 * 24 * 60 * 60);
                String sessionId = session.getId();
                AppUserInfo appUserInfo = new AppUserInfo();
                BeanUtils.copyProperties(dbUser, appUserInfo);
                ResultModel countResult = deviceServices.queryUserDeviceCount(appUserInfo.getId());
                if (countResult.isBresult()) {
                    appUserInfo.setDevCount((int) countResult.getCount());
                }
                DeviceConfig deviceConfig = deviceServices.queryDeviceConfig(dbUser.getId());
                if (deviceConfig != null) {
                    appUserInfo.setSord(deviceConfig.getSord());
                }
                appUserInfo.setSessionId(sessionId);
                userSessionInfo.setLoginUser(dbUser);
                session.setAttribute(ConstantCode.USERINFO_KEY, userSessionInfo);
                return new JsonResult().Serailze(appUserInfo);
                // return new JsonResult().Serailze(dbUser);
            } else {
                return new JsonResult().Serailze(false, ErrorCode.LOGINDEFAUT_ISENABLE);
            }

        } else {
            return new JsonResult().Serailze(false, ErrorCode.LOGIN_DEFAULT);
        }
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/exit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String exitLogin(HttpSession session) {
        ResultModel resultModel = new ResultModel();
        session.removeAttribute(ConstantCode.USERINFO_KEY);
        resultModel.setBresult(true);
        resultModel.setMessage(SuccessCode.EXIT_SUCC);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台
     * 对用户信息修改
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String edit(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.doedit(user);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台
     * 对用户信息修改(头像，昵称，邮箱，等）
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String editapp(@RequestBody String data, HttpSession session) {
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (sessionuser != null && sessionuser.getLoginUser() != null) {
            User user = JsonStringUtil.readValue(data, User.class);
            user.setId(sessionuser.getLoginUser().getId());
            if (user.getEmail() != null && !user.getEmail().equals("")) {
                if (!Verification.checkEmaile(user.getEmail())) {
                    resultModel.setMessage("邮箱格式不正确！");
                    resultModel.setBresult(false);
                    return new JsonResult().Serailze(resultModel);
                }
            }

            resultModel = userServices.doAppEdit(user);
            if (resultModel.isBresult()) {
                user = (User) resultModel.getObject();
                sessionuser.setLoginUser(user);
                AppUserInfo appUserInfo = new AppUserInfo();
                BeanUtils.copyProperties(user, appUserInfo);
                ResultModel countResult = deviceServices.queryUserDeviceCount(user.getId());
                if (countResult.isBresult()) {
                    appUserInfo.setDevCount((int) countResult.getCount());
                }
                appUserInfo.setSessionId(session.getId());
                session.setAttribute(ConstantCode.USERINFO_KEY, sessionuser);
                resultModel.setBresult(true);
                resultModel.setObject(appUserInfo);
            }
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }

        return new JsonResult().Serailze(resultModel);
    }

    /**
     * APP端用户查看自己个人信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/app/query", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String searchLogin(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            user = userServices.queryById(user.getId());
            AppUserInfo appUserInfo = new AppUserInfo();
            BeanUtils.copyProperties(user, appUserInfo);
            ResultModel countResult = deviceServices.queryUserDeviceCount(user.getId());
            if (countResult.isBresult()) {
                appUserInfo.setDevCount((int) countResult.getCount());
            }
            Device device = deviceServices.queryDeviceByUserId(user.getId());
            if (device != null) {
                appUserInfo.setCurrentDevice(device);
                appUserInfo.setDevicekey(device.getDevicekey());
            }
            appUserInfo.setIntAccount(user.getIntAccount());
            appUserInfo.setSessionId(session.getId());
            appUserInfo.setBindDevices(deviceServices.queryDevicesByUser(user.getId()));
            resultModel.setBresult(true);
            resultModel.setObject(appUserInfo);

        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * web端用户查看自己个人信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/queryuserinfo", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String searchuserinfo(@RequestBody String data, HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = JsonStringUtil.readValue(data, User.class);
            User users = userServices.queryById(user.getId());
            resultModel.setBresult(true);
            resultModel.setObject(users);
            return new JsonResult().Serailze(resultModel);

        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按照条件查找用户
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = userServices.queryUsers(conditions);
        resultModel.setBresult(true);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 删除
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String deleteUser(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.doDeleteUser(user);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * APP端修改名称
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/modify/username", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyUserName(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.doModifyUserName(user);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 启用
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/enable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String enableUser(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.doEnableUser(user);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 修改密码
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/modify/password", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyPassword(@RequestBody String data) {
        UserPasswordVo userPasswordVo = JsonStringUtil.readValue(data, UserPasswordVo.class);
        if (userPasswordVo.getModifyPassword().equals(userPasswordVo.getUser().getUpassword())) {
            ResultModel resultModel = userServices.doModifyPassword(userPasswordVo.getUser());
            return new JsonResult().Serailze(resultModel);
        } else {
            return new JsonResult().Serailze(false, ErrorCode.PASSWORD_DEFAULT);
        }
    }

    /**
     * 查询某台设备的积分历史
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/score", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScore(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.queryUserTotalScore(user.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 模糊查询人员信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/like/phone", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserByPhone(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        String phone = (String) pageCondition.getByKey("phone");
        ResultModel resultModel = userServices.queryByPartName(phone, pageCondition.getPage());
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 登录的用户查询信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/login/user", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String loginUser(HttpSession session) {
        ResultModel resultModel = new ResultModel();
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (userSessionInfo != null) {
            WebUserInfoVo webUserInfoVo = new WebUserInfoVo();
            webUserInfoVo.setUser(userSessionInfo.getLoginUser());
            webUserInfoVo.setUserLoginHistory(userSessionInfo.getHistoryLogins());

            resultModel.setObject(webUserInfoVo);
            resultModel.setBresult(true);
            return new JsonResult().Serailze(resultModel);
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
            return new JsonResult().Serailze(resultModel);
        }
    }

    /**
     * 查询所有账户未被禁用的用户
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/enable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryEnable(@RequestBody String data) {
        Page page = JsonStringUtil.readValue(data, Page.class);
        ResultModel resultModel = userServices.queryByEnable(page);
        resultModel.setBresult(true);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 根据ID查询指定用户
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/query/only", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryById(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = new ResultModel();
        User user1 = userServices.queryById(user.getId());
        resultModel.setBresult(true);
        resultModel.setObject(user1);
        return new JsonResult().Serailze(user1);
    }

    @RequestMapping(value = "/query/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String testAdd() {

        for (int i = 0; i < 500; i++) {
            User user = new User();
            user.setPhone("183xxx" + i);
            user.setUsername("wl" + i);
            userServices.doAdd(user);
        }
        return new JsonResult().Serailze(new ResultModel(true));
    }


    /**
     * 绑定int账户
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/modify/bindint", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyBindint(@RequestBody String data, HttpSession session) {
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = sessionuser.getLoginUser().getId();
        User user = JsonStringUtil.readValue(data, User.class);
        user.setId(userId);
        ResultModel resultModel = userServices.doBindIntAccount(user);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 解绑int账户
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/modify/untiedint", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyUntiedint(HttpSession session) {
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = sessionuser.getLoginUser().getId();
        User user = new User();
        user.setId(userId);
        ResultModel resultModel = userServices.doUnitedIntAccount(user);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 用户新增个数
     * 设备激活个数
     * 昨日产生总积分
     */
    @RequestMapping(value = "/udscount", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryudxcount() {
        ResultModel resultModel = userServices.queryUdsCount();
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 获取该用户下的邀请送任务
     */
    @RequestMapping(value = "/invite/{userTag}", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryinvite(@PathVariable("userTag") String userTag, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setContentType("application/json");
        User user = userServices.queryUserByTag(userTag);
        ResultModel resultModel = invitetServices.queryInviteList(user.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 获取int提取记录
     */
    @RequestMapping(value = "/intchengedetail", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String intchengedetail(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = userServices.queryIntDetailList(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改用户密码
     */
    @RequestMapping(value = "/updupass", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifyupass(@RequestBody String data, HttpSession session) {
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = sessionuser.getLoginUser().getId();
        PasswordVo passwordVo = JsonStringUtil.readValue(data, PasswordVo.class);
        ResultModel resultModel = userServices.doEditPass(passwordVo,userId);
        return new JsonResult().Serailze(resultModel);
    }


}
